//package com.tianjishuju.cimiss.station.station.util;
//
//import com.sun.jersey.api.client.*;
//import com.sun.jersey.api.client.WebResource.Builder;
//import com.sun.jersey.api.client.async.TypeListener;
//import com.sun.jersey.api.representation.Form;
//import org.codehaus.jettison.json.JSONObject;
//
//import javax.ws.rs.core.MediaType;
//import javax.ws.rs.core.MultivaluedMap;
//import java.io.BufferedInputStream;
//import java.util.Map;
//import java.util.concurrent.ExecutionException;
//import java.util.concurrent.Future;
//
//public class JerseyHttpUtil {
//
//	private Client client = Client.create();
//
//	public static JerseyHttpUtil instance = new JerseyHttpUtil();
//
//	public <T> T postForm(String url, Class<T> beanClass) {
//		return this.postFormUrl(url, null, beanClass);
//	}
//
//	public <T> T postFormWithHead(String url, Map<String, Object> headMap, Class<T> beanClass) {
//		return this.postFormUrlWithHead(url, null, null, headMap, beanClass);
//	}
//
//	/**
//	 *
//	 * @param url
//	 * @param postData
//	 * 			  <br> 使用以下对象中 isWriteable 方法中对象
//	 *            <br> application/x-www-form-urlencoded = [FormProvider,FormMultivaluedMapProvider]
//	 * @return
//	 */
//	public <T> T postFormUrl(String url, MultivaluedMap<String, String> postData, Class<T> beanClass) {
//		return this.postFormUrlWithHead(url, null, postData, null, beanClass);
//	}
//
//	public <T> T postFormUrlWithHead(String url, MultivaluedMap<String, String> postData, Map<String, Object> headMap, Class<T> beanClass) {
//		return this.postFormUrlWithHead(url, null, postData, headMap, beanClass);
//	}
//
//	public <T> T postFormUrlWithHead(String url, MultivaluedMap<String, String> paramMap, MultivaluedMap<String, String> postData, Map<String, Object> headMap, Class<T> beanClass) {
//		return this.connection(url, "POST", paramMap, postData, headMap, MediaType.APPLICATION_FORM_URLENCODED_TYPE, beanClass);
//	}
//
//	/**
//	 *
//	 * @param url
//	 * @param postData
//	 * 			  <br> 使用以下对象中 isWriteable 方法中对象
//	 *            <br> *\* =[FormProvider,StringProvider,ByteArrayProvider,FileProvider,InputStreamProvider,DataSourceProvider,XMLJAXBElementProvider,ReaderProvider,
//	 *            <br> 	  DocumentProvider,StreamingOutputProvider,SourceProvider,FastJsonProvider,XMLRootElementProvider,XMLListElementProvider]
//	 * @return
//	 */
//	public <T> T postFormData(String url, Form postData, Class<T> beanClass) {
//		return this.postFormDataWithHead(url, null, postData, null, beanClass);
//	}
//
//	public <T> T postFormDataWithHead(String url, Form postData, Map<String, Object> headMap, Class<T> beanClass) {
//		return this.postFormDataWithHead(url, null, postData, headMap, beanClass);
//	}
//
//	public <T> T postFormDataWithHead(String url, MultivaluedMap<String, String> paramMap, Form postData, Map<String, Object> headMap, Class<T> beanClass) {
//		return this.connection(url, "POST", paramMap, postData, headMap, MediaType.MULTIPART_FORM_DATA_TYPE, beanClass);
//	}
//
//	public <T> T postRaw(String url, Class<T> beanClass) {
//		return this.postRawWithHead(url, null, null, null, beanClass);
//	}
//
//	public <T> T postRaw(String url, Object postData, Class<T> beanClass) {
//		return this.postRawWithHead(url, null, postData, null, beanClass);
//	}
//
//	public <T> T postRaw(String url, MultivaluedMap<String, String> paramMap, Object postData, Class<T> beanClass) {
//		return this.postRawWithHead(url, paramMap, postData, null, beanClass);
//	}
//
//	public <T> T postRawWithHead(String url, Map<String, Object> headMap, Class<T> beanClass) {
//		return this.postRawWithHead(url, null, null, headMap, beanClass);
//	}
//
//	public <T> T postRawWithHead(String url, Object postData, Map<String, Object> headMap, Class<T> beanClass) {
//		return this.postRawWithHead(url, null, postData, headMap, beanClass);
//	}
//
//	public <T> T postRawWithHead(String url, MultivaluedMap<String, String> paramMap, Object postData, Map<String, Object> headMap, Class<T> beanClass) {
//		return this.connection(url, "POST", paramMap, postData, headMap, MediaType.APPLICATION_JSON_TYPE, beanClass);
//	}
//
//
//	public <T> T get(String url, Class<T> beanClass) {
//		return this.get(url, null, beanClass);
//	}
//
//	public <T> T get(String url, MultivaluedMap<String, String> paramMap, Class<T> beanClass) {
//		return this.getWithHead(url, paramMap, null, beanClass);
//	}
//    public ClientResponse get(String url, MultivaluedMap<String, String> paramMap) {
//        return this.getWithHead(url, paramMap, null);
//    }
//
//	public <T> T getWithHead(String url, MultivaluedMap<String, String> paramMap, Map<String, Object> headMap, Class<T> beanClass) {
//		return this.connection(url, "GET", paramMap, null, headMap, null, beanClass);
//	}
//    public ClientResponse getWithHead(String url, MultivaluedMap<String, String> paramMap, Map<String, Object> headMap) {
//        return this.connection(url, "GET", paramMap, null, headMap, null);
//    }
//
//	public <T> T delete(String url, Class<T> beanClass) {
//		return this.connection(url, "DELETE", null, null, null, null, beanClass);
//	}
//
//	/**
//	 *
//	 * @param url
//	 * @param method
//	 * @param paramMap
//	 *            <br> paramMap Object example :
//	 *            <br> MultivaluedMap<String, String> paramMap = new MultivaluedMapImpl();
//	 *            <br> paramMap.add("userId", "2345");
//	 * @param postData
//	 * 			  <br> 使用以下对象中 isWriteable 方法中对象
//	 *            <br> text/xml = [XMLJAXBElementProvider,DocumentProvider,SourceProvider,XMLRootElementProvider,XMLListElementProvider]
//	 *            <br> application/octet-stream = [ByteArrayProvider,FileProvider,InputStreamProvider,DataSourceProvider,StreamingOutputProvider]
//	 *            <br> application/xml = [XMLJAXBElementProvider,DocumentProvider,SourceProvider,XMLRootElementProvider,XMLListElementProvider]
//	 *            <br> application/x-www-form-urlencoded = [FormProvider,FormMultivaluedMapProvider]
//	 *            <br> *\* =[FormProvider,StringProvider,ByteArrayProvider,FileProvider,InputStreamProvider,DataSourceProvider,XMLJAXBElementProvider,ReaderProvider,
//	 *            <br>   DocumentProvider,StreamingOutputProvider,SourceProvider,FastJsonProvider,XMLRootElementProvider,XMLListElementProvider,JSONObject,JSONArray]
//	 *            <br> Map
//	 * @param headMap
//	 * @param contentType
//	 * @return
//	 */
//	@SuppressWarnings("rawtypes")
//	private <T> T connection(String url, String method, MultivaluedMap<String, String> paramMap, Object postData, Map<String, Object> headMap, MediaType contentType, Class<T> beanClass) {
//		ClientResponse response = null;
//		try {
//			if(client == null) client = Client.create();
//
//			WebResource webResource = client.resource(url);
//
//			if(paramMap != null) webResource = webResource.queryParams(paramMap);
//
//			Builder builder = webResource.getRequestBuilder();
//
//			if(contentType != null) builder.type(contentType);
//
//			if(headMap != null && !headMap.isEmpty()) {
//				for(String str: headMap.keySet())
//					builder.header(str, headMap.get(str));
//			}
//
//			if("POST".equals(method)) {
//				if(postData instanceof Map) {
//					postData = new JSONObject((Map) postData);
//				}
//
//				response = builder.post(ClientResponse.class, postData);
//			} else if("GET".equals(method)){
//				response = builder.get(ClientResponse.class);
//			} else if("DELETE".equals(method)){
//				response = builder.delete(ClientResponse.class);
//			} else {
//				return null;
//			}
//
//			if(ObjUtil.isNotEmpty(response)) {
//				if(String.class.equals(beanClass)) {
//					return response.getEntity(beanClass);
//				}
//				if (BufferedInputStream.class.equals(beanClass)){
//                    return response.getEntity(beanClass);
//                }
//				String resultStr = response.getEntity(String.class);
//				return ConvertUtil.jsonStrToObject(resultStr, beanClass);
//			}
//		} catch (Exception e) {
//			if(e instanceof ClientHandlerException) {
//				System.out.println("=============== 链接失败 ===========");
//			}
//			e.printStackTrace();
//		} finally {
//			if(response != null){
//                try {
//                    Thread.sleep(3000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                response.close();
//            }
//		}
//
//		return null;
//	}
//    private ClientResponse connection(String url, String method, MultivaluedMap<String, String> paramMap, Object postData, Map<String, Object> headMap, MediaType contentType) {
//        ClientResponse response = null;
//        try {
//            if (client == null) client = Client.create();
//
//            WebResource webResource = client.resource(url);
//
//            if (paramMap != null) webResource = webResource.queryParams(paramMap);
//
//            Builder builder = webResource.getRequestBuilder();
//
//            if (contentType != null) builder.type(contentType);
//
//            if (headMap != null && !headMap.isEmpty()) {
//                for (String str : headMap.keySet())
//                    builder.header(str, headMap.get(str));
//            }
//
//            if ("POST".equals(method)) {
//                if (postData instanceof Map) {
//                    postData = new JSONObject((Map) postData);
//                }
//
//                response = builder.post(ClientResponse.class, postData);
//            } else if ("GET".equals(method)) {
//                response = builder.get(ClientResponse.class);
//            } else if ("DELETE".equals(method)) {
//                response = builder.delete(ClientResponse.class);
//            } else {
//                return null;
//            }
//        }catch (Exception e){
//            if(e instanceof ClientHandlerException) {
//                System.out.println("=============== 链接失败 ===========");
//            }
//            e.printStackTrace();
//        }
//        return response;
//    }
//
//	/******************   async http start    ********************/
//
//	public void asyncPostRaw(String url, MultivaluedMap<String, String> paramMap, Object postData, JerseyAnsycHttpCallback<?> callback) {
//		this.asyncPostRawWithHead(url, paramMap, postData, null, callback);
//	}
//
//	public void asyncPostRawWithHead(String url, MultivaluedMap<String, String> paramMap, Object postData, Map<String, Object> headMap, JerseyAnsycHttpCallback<?> callback) {
//		this.asyncConnection(url, "POST", paramMap, postData, headMap, MediaType.APPLICATION_JSON_TYPE, callback);
//	}
//
//	public void asyncGet(String url, MultivaluedMap<String, String> paramMap, JerseyAnsycHttpCallback<?> callback) {
//		this.asyncGetWithHead(url, paramMap, null, callback);
//	}
//
//	public void asyncGetWithHead(String url, MultivaluedMap<String, String> paramMap, Map<String, Object> headMap, JerseyAnsycHttpCallback<?> callback) {
//		this.asyncConnection(url, "GET", paramMap, null, headMap, MediaType.APPLICATION_JSON_TYPE, callback);
//	}
//
//	/**
//	 *
//	 * @param url
//	 * @param method
//	 * @param paramMap
//	 *            <br> paramMap Object example :
//	 *            <br> MultivaluedMap<String, String> paramMap = new MultivaluedMapImpl();
//	 *            <br> paramMap.add("userId", "2345");
//	 * @param postData
//	 * 			  <br> 使用以下对象中 isWriteable 方法中对象
//	 *            <br> text/xml = [XMLJAXBElementProvider,DocumentProvider,SourceProvider,XMLRootElementProvider,XMLListElementProvider]
//	 *            <br> application/octet-stream = [ByteArrayProvider,FileProvider,InputStreamProvider,DataSourceProvider,StreamingOutputProvider]
//	 *            <br> application/xml = [XMLJAXBElementProvider,DocumentProvider,SourceProvider,XMLRootElementProvider,XMLListElementProvider]
//	 *            <br> application/x-www-form-urlencoded = [FormProvider,FormMultivaluedMapProvider]
//	 *            <br> *\* =[FormProvider,StringProvider,ByteArrayProvider,FileProvider,InputStreamProvider,DataSourceProvider,XMLJAXBElementProvider,ReaderProvider,
//	 *            <br>   DocumentProvider,StreamingOutputProvider,SourceProvider,FastJsonProvider,XMLRootElementProvider,XMLListElementProvider,JSONObject,JSONArray]
//	 *            <br> Map
//	 * @param headMap
//	 * @param contentType
//	 * @return
//	 */
//	@SuppressWarnings("rawtypes")
//	private void asyncConnection(String url, String method, MultivaluedMap<String, String> paramMap, Object postData, Map<String, Object> headMap, MediaType contentType, JerseyAnsycHttpCallback<?> callback) {
//		if(client == null) client = Client.create();
//
//		AsyncWebResource webResource = client.asyncResource(url);
//
//		if(paramMap != null) webResource = webResource.queryParams(paramMap);
//
//		AsyncWebResource.Builder builder = webResource.getRequestBuilder();
//
//		if(contentType != null) builder.type(contentType);
//
//		if(headMap != null && !headMap.isEmpty()) {
//			for(String str: headMap.keySet())
//				builder.header(str, headMap.get(str));
//		}
//
//		if(ObjUtil.isEmpty(callback)){
//			if("POST".equals(method)) {
//				if(postData instanceof Map) {
//					postData = new JSONObject((Map) postData);
//				}
//				builder.post(postData);
//			} else if("GET".equals(method)){
//				builder.get(ClientResponse.class);
//			} else if("DELETE".equals(method)){
//				builder.delete();
//			} else {
//				return;
//			}
//			return;
//		}
//
//		if("POST".equals(method)) {
//			builder.post(new TypeListener<ClientResponse>(ClientResponse.class) {
//				@Override
//				public void onComplete(Future<ClientResponse> f) throws InterruptedException {
//					printAsyncResult(f, callback);
//				}
//			}, postData);
//		} else if("GET".equals(method)){
//			builder.get(new TypeListener<ClientResponse>(ClientResponse.class) {
//				@Override
//				public void onComplete(Future<ClientResponse> f) throws InterruptedException {
//					printAsyncResult(f, callback);
//				}
//			});
//		} else if("DELETE".equals(method)){
//			builder.delete(new TypeListener<ClientResponse>(ClientResponse.class) {
//				@Override
//				public void onComplete(Future<ClientResponse> f) throws InterruptedException {
//					printAsyncResult(f, callback);
//				}
//			});
//		} else {
//			return;
//		}
//		return;
//	}
//
//	private void printAsyncResult(Future<ClientResponse> f, JerseyAnsycHttpCallback<?> callback) throws InterruptedException {
//		ClientResponse response = null;
//		try {
//			response = f.get();
//			String resultStr = response.getEntity(String.class);
//			if (response == null || response.getStatus() != 200) {
//				System.out.println("=============== 链接失败 ===========");
//				System.err.println(" Async "+response.getStatus());
//			} else{
//				System.out.println(" Async "+response.getStatus()+" "+resultStr);
//			}
//			callback.result(resultStr);
//		} catch (ExecutionException e) {
//			e.printStackTrace();
//		} finally {
//			if(response != null) response.close();
//		}
//	}
//}
